package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.OrderBillItem;
import cn.wolfcode.wms.domain.Orderbill;
import cn.wolfcode.wms.mapper.OrderBillItemMapper;
import cn.wolfcode.wms.mapper.OrderbillMapper;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IOrderbillService;
import cn.wolfcode.wms.util.PageResult;
import cn.wolfcode.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class OrderbillServiceImpl implements IOrderbillService {
    @Autowired
    private OrderbillMapper orderbillMapper;
    @Autowired
    private OrderBillItemMapper orderBillItemMapper;

    @Override
    public void saveOrUpdate(Orderbill entity) {
        if (entity.getId() != null) {
            updateBill(entity);
        } else {
           saveBill(entity);
        }
    }
    //更新单据的业务操作
    private  void updateBill(Orderbill entity){
        //判断当前单据必须是为审核状态
        Orderbill old = orderbillMapper.selectByPrimaryKey(entity.getId());
        //删除旧的明细插入新的明细
        orderbillMapper.deleItemsByBillId(entity.getId());
        if (old.getStatus() ==0) {
            BigDecimal totalNumber = BigDecimal.ZERO;
            BigDecimal totalAmount = BigDecimal.ZERO;
            for (OrderBillItem item : entity.getItems()) {
                BigDecimal number = item.getNumber();
                BigDecimal amount = item.getCostPrice().multiply(number).setScale(2,RoundingMode.HALF_UP);
                totalNumber =totalNumber.add(number);
                totalAmount = totalAmount.add(amount);

                item.setBillId(entity.getId());
                item.setTotalAmount(amount);
                orderBillItemMapper.insert(item);
                //保存到数据库
            }
            old.setTotalNumber(totalNumber);
            old.setTotalAmount(totalAmount);
            old.setSn(entity.getSn());
            old.setVdate(entity.getVdate());
            old.setSupplier(entity.getSupplier());
            orderbillMapper.updateByPrimaryKey(old);
        }
        //将新的数据设置到old
    }

    //新增单据的业务操作
    private  void saveBill(Orderbill entity){
        //1.状态 2.录入人 3.录入时间
        entity.setStatus(Orderbill.NOTAUDIT);
        entity.setInputUser(UserContext.getCurrentUser());
        entity.setInputTime(new Date());
        //entity.getId()
        //2:重新后台计算明细的小计
        BigDecimal totalNumber = BigDecimal.ZERO;
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (OrderBillItem item : entity.getItems()) {
            BigDecimal number = item.getNumber();
            BigDecimal amount = item.getCostPrice().multiply(number).setScale(2,RoundingMode.HALF_UP);
            totalNumber =totalNumber.add(number);
            totalAmount = totalAmount.add(amount);

            //保存到数据库
        }
        entity.setTotalNumber(totalNumber);
        entity.setTotalAmount(totalAmount);
            orderbillMapper.insert(entity);

        //先保存订单在保存明细
        for (OrderBillItem item : entity.getItems()) {
            item.setBillId(entity.getId());
            BigDecimal number = item.getNumber();
            BigDecimal amount = item.getCostPrice().multiply(number).setScale(2,RoundingMode.HALF_UP);
            item.setTotalAmount(amount);
            orderBillItemMapper.insert(item);
        }

    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            orderbillMapper.deleItemsByBillId(id);
            orderbillMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Orderbill get(Long id) {

            Orderbill entity = orderbillMapper.selectByPrimaryKey(id);
        return entity;

    }

    @Override
    public List<Orderbill> selectall() {
        List<Orderbill> depts = orderbillMapper.selectAll();
        return depts;
    }

    @Override
    //审核
    public void audit(Long id) {
        //审核人 审核时间 状态
        //同过传入id确定指定订单
        Orderbill orderbill = new Orderbill();
        orderbill.setId(id);
        orderbill.setAuditor(UserContext.getCurrentUser());
        orderbill.setAuditTime(new Date());
        orderbill.setStatus(Orderbill.AUDIT);

        orderbillMapper.audit(orderbill);
    }

    @Override
    public PageResult queryForList(QueryObject qo) {
        int rows = orderbillMapper.rows(qo);
        if (rows == 0) {
            return PageResult.EMPTY_RESULT;

        }
        PageResult result = new PageResult(qo.getCurrentPage(), qo.getPageSize(), rows, orderbillMapper.queryForList(qo));
        System.out.println(66);
        return result;

    }
}
